package com.ldp.designpatterns.study.strategy.context;

import com.ldp.designpatterns.study.strategy.service.MathOperation;

/**
 * @ClassName Context TODO:创建Context类
 * @Description TODO 策略模式——每个算法都需要一个对应的策略类,是一种非常有用的设计模式
 * @Author lindipei
 * @Date 2023/10/18 17:05
 */
public class Calculator {
    private MathOperation operation;

    public void setOperation(MathOperation operation) {
        this.operation = operation;
    }

    public int performOperation(int a, int b) {
        if (operation != null) {
            return operation.operate(a, b);
        }
        throw new IllegalStateException("No operation set");
    }
}
